/*
 * Enrollment.java
 *
 * This class demonstrates the use of assertions.
 *
 * 
 */

package com.javacore.day9.gym;

import java.util.InputMismatchException;
import java.util.Scanner;

/**
 * This class uses various assertions to ensure that data entered or computed
 * is valid.
 * @author 
 */
public class Enrollment {
    
    /** Creates a new instance of Enrollment. */
    public Enrollment() {
    }
    
    /**
     * Method to accept member details.
     */
    public void setMemberDetails() {
        
        Scanner input = new Scanner(System.in);
        //Scanner in=new Scanner(System.in);
        //input.useDelimiter("\n");
        
        try {
        	//int k=in.nextInt();
        	//System.out.println(k);
            // Accept membership ID
            System.out.println("Enter membership ID:" );
            membershipID = input.nextInt();
            
            // Accept member name
            System.out.println("Enter member name:");
            memberName = input.next();
            
            // Accept member age
            System.out.println("Enter member age:" );
            age = input.nextInt();
            
            
            // Accept membership type
            System.out.println("Enter membership type. Choose one from " +
                    "the following:\n1. Silver \n2. Gold\n3. Platinum");
            membershipType = input.nextInt();
            
            // Accept membership duration
            System.out.println("Enter membership duration(years):" );
            duration = input.nextInt();
            
        } catch(InputMismatchException e) {
            System.out.println("Exception occurred - Data type mismatch. " +
                    "Try again...");
            return;
        }
    }
    
    /**
     * Method to calculate the basic fees.
     * @return Returns the basic fees
     */
    private float computeFee() {
        
        // Variable to store discount percentage on the basis of age
        float discount;
        
        // Variable to store membership fees
        float fees = 0;
        
        // Determine the fees payable
        switch(membershipType) {
            // Silver membership
            case 1:
                fees = 4000;
                break;
                // Gold membership
            case 2:
                fees = 7000;
                break;
                // Platinum membership
            case 3:
                fees = 10000;
                break;
            default:
                // Assertion to check control flow invariant
                assert false: "Incorrect option for membership type.";
        }
        if (age > 45) {
            discount = 10;
        } else {
            discount =  3;
        }
        //calculate fees after discount
        fees =  fees - ((discount / 100) * fees);
        return fees;
    }
    
    /**
     * Method to display facilities offered to the customer.
     * @return Returns the perks offered
     */
    private String displayFacilities() {
        
        
        if(membershipType == 1) {
            return "This membership includes only basic facilities.";
        }
        
        if(membershipType == 2) {
            return "This membership additionally includes spa and sauna.";
        }
        
        if (membershipType == 3) {
            return "This membership additionally includes Power Yoga " +
                    "and Kickboxing.";
        }
        
        return "Error";
    }
    
    /**
     * Method to calculate the bill amount.
     * @param fees Parameter containing the basic fees which is used
     * to calculate bill amount
     * @return Returns the bill amount
     */
    private float computeBill(float fees) {
        
        
        // Variable to store bill amount, default bill amount is 1000
        float billAmount = 1000;
        
        billAmount = duration * fees;
        
        
        return billAmount;
    }
    
    /**
     * Method to display member details such as fees and bill amount.
     */
    public void getMemberDetails() {
        Scanner input = new Scanner(System.in);
        
        float fees = computeFee();
        System.out.println("Basic Fee amount is: " + fees);
        
        System.out.println("Do you wish to calculate bill now? Enter y/n:" );
        String answer = input.next();
        
        if (answer.equalsIgnoreCase("y")) {
            System.out.println("Bill Amount is: " + computeBill(fees));
            String perks = displayFacilities();
            System.out.println(perks);
        }
    }
    
    /** Instance variable to store member number. */
    private int membershipID;
    
    /** Instance variable to store name of the member. */
    private String memberName;
    
    /** Instance variable to store age of the member. */
    private int age;
    
    /** Instance variable to store membership type. */
    private int membershipType;
    
    /** Instance variable to store duration of membership in years. */
    private int duration;
    
    /** Instance variable to store facilities offered. */
    private String perks ;
}
